package com.pcxm.cm.util;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Random;

public class StringUtil
{

    public StringUtil()
    {
    }

    public static String dealEmpty(String str)
    {
        String returnStr = "";
        for(int i = 0; i < str.length(); i++)
        {
            String temp = str.substring(i, i + 1);
            if(!temp.equals(" "))
                returnStr = (new StringBuilder(String.valueOf(returnStr))).append(temp).toString();
        }

        return returnStr;
    }
    
    public static String appendEmptyStr(String str, int len)
    {
    	StringBuilder sb = new StringBuilder();
    	int miuLen = len - str.length();
    	if(miuLen > 0)
    	{
    		for(int i=0; i<miuLen; i++)
    		{
    			sb.append(" ");
    		}
    	}
    	sb.append(str);
    	
    	return sb.toString();
    }

    public static String dealNull(String str)
    {
        if(str == null)
            return "";
        else
            return str;
    }

    public static String GBKToISO(String str)
    {
    	try {
    		return new String(str.getBytes("GBK"), "ISO8859_1");
    	} catch(Exception ex) {
    	}
        return null;
    }

    public static String ISOToGBK(String str)
    {
    	try {
    		return (new String(str.getBytes("ISO8859_1"), "GBK")).trim();
    	} catch(Exception ex) {
    		
    	}
        
        return null;
    }

    public static String getRightString(String src)
    {
        String rightStr = "";
        try
        {
            rightStr = new String(src.getBytes("ISO8859_1"));
        }
        catch(Exception exception) { }
        return rightStr;
    }

    public static String getXlsString(String src)
    {
        String rightStr = "";
        try
        {
            rightStr = new String(src.getBytes("UTF-16BE"));
        }
        catch(Exception exception) { }
        return rightStr;
    }

    public static String getFixedStrRight(String str, int length)
    {
        if(str == null)
            str = "";
        String resultStr = str;
        try
        {
            if(str != null && !"".equals(str))
            {
                int strByteLength = str.getBytes("GB2312").length;
                if(strByteLength < length)
                {
                    int count = length - strByteLength;
                    for(int i = 0; i < count; i++)
                        resultStr = (new StringBuilder(String.valueOf(resultStr))).append(" ").toString();

                } else
                if(strByteLength > length)
                {
                    byte byteArray[] = new byte[length];
                    byte temp[] = str.getBytes("GB2312");
                    int n = 0;
                    for(int i = 0; i < length; i++)
                    {
                        byteArray[i] = temp[i];
                        if(temp[i] < 0)
                            n++;
                    }

                    if(n % 2 == 1)
                        byteArray[length - 1] = 32;
                    resultStr = new String(byteArray);
                }
            } else
            {
                for(int i = 0; i < length; i++)
                    resultStr = (new StringBuilder(String.valueOf(resultStr))).append(" ").toString();

            }
        }
        catch(UnsupportedEncodingException e)
        {
            System.out.println((new StringBuilder("\u5B57\u7B26\u7684\u7F16\u7801\u65B9\u5F0F\u4E0D\u5BF9")).append(e.getMessage()).toString());
        }
        return resultStr;
    }

    public static String getFixedStrLeft(String str, int length)
    {
        if(str == null)
            str = "";
        String resultStr = str;
        try
        {
            if(str != null && !"".equals(str))
            {
                int strByteLength = str.getBytes("GB2312").length;
                if(strByteLength < length)
                {
                    int count = length - strByteLength;
                    for(int i = 0; i < count; i++)
                        resultStr = (new StringBuilder("0")).append(resultStr).toString();

                } else
                if(strByteLength > length)
                {
                    byte byteArray[] = new byte[length];
                    byte temp[] = str.getBytes("GB2312");
                    int n = 0;
                    for(int i = 0; i < length; i++)
                    {
                        byteArray[i] = temp[i];
                        if(temp[i] < 0)
                            n++;
                    }

                    if(n % 2 == 1)
                        byteArray[length - 1] = 32;
                    resultStr = new String(byteArray);
                }
            } else
            {
                for(int i = 0; i < length; i++)
                    resultStr = (new StringBuilder(String.valueOf(resultStr))).append(" ").toString();

            }
        }
        catch(UnsupportedEncodingException e)
        {
            System.out.println((new StringBuilder("\u5B57\u7B26\u7684\u7F16\u7801\u65B9\u5F0F\u4E0D\u5BF9")).append(e.getMessage()).toString());
        }
        return resultStr;
    }

    public static String sqlStrchop(String str)
    {
        String reStr = "";
        if(str != null && !str.equals(""))
        {
            reStr = str.replaceAll("\"", "\u201C");
            reStr = reStr.replaceAll("'", "\u2019");
        }
        return reStr;
    }

    public static String getStr(String str)
    {
        String temp;
        String temp_p = str;
        try {
			temp = new String(temp_p.getBytes("ISO8859_1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
        temp = sqlStrchop(temp);
        return temp;
    }

    public static final String genRandomStr(int length)
    {
        String sRand = "";
        Random random = new Random();
        for(int i = 0; i < length; i++)
        {
            String rand = String.valueOf(random.nextInt(10));
            sRand = (new StringBuilder(String.valueOf(sRand))).append(rand).toString();
        }

        return sRand;
    }

    public String formatDouble(double src)
    {
        return ROUND.format(src);
    }

    public static String diffor(String str1, String str2)
    {
        int snNum[] = new int[str1.length()];
        String result = "";
        String temp = "";
        int i = 0;
        for(int j = 0; i < str1.length(); j++)
        {
            if(j == str2.length())
                j = 0;
            snNum[i] = str1.charAt(i) ^ str2.charAt(j);
            i++;
        }

        for(int k = 0; k < str1.length(); k++)
        {
            if(snNum[k] < 10)
                temp = (new StringBuilder("00")).append(snNum[k]).toString();
            else
            if(snNum[k] < 100)
                temp = (new StringBuilder("0")).append(snNum[k]).toString();
            result = (new StringBuilder(String.valueOf(result))).append(temp).toString();
        }

        return result;
    }

    public static String ResDiffor(String str1, String str2)
    {
        char snNum[] = new char[str1.length() / 3];
        String result = "";
        int i = 0;
        for(int j = 0; i < str1.length() / 3; j++)
        {
            if(j == str2.length())
                j = 0;
            int n = Integer.parseInt(str1.substring(i * 3, i * 3 + 3));
            snNum[i] = (char)((char)n ^ str2.charAt(j));
            i++;
        }

        for(int k = 0; k < str1.length() / 3; k++)
            result = (new StringBuilder(String.valueOf(result))).append(snNum[k]).toString();

        return result;
    }

    public static boolean isSimpleStr(String str)
    {
        str = str.toLowerCase();
        if(str != null && !"".equals(str))
        {
            char firstch = str.charAt(0);
            int samecnt = 1;
            int seqcnt = 1;
            for(int i = 1; i < str.length(); i++)
            {
                if(str.charAt(i) == firstch)
                    samecnt++;
                int frontint = str.charAt(i - 1);
                int chint = str.charAt(i);
                if(frontint - chint == -1 || frontint - chint == 1)
                    seqcnt++;
            }

            if(samecnt == str.length())
                return true;
            if(seqcnt == str.length())
                return true;
        }
        return false;
    }

    private static final DecimalFormat ROUND = new DecimalFormat("0.00");

}
